package com.zmn.oms.services.impl.datasync;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.zmn.base.oms.common.consts.MqTagDataSyncConsts;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.mq.interfaces.ZmnMQSender;
import com.zmn.oms.services.interfaces.datasync.ElasticSearchDataSyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 类描述：es数据同步
 *
 * @author heciqi
 * @date 2022/03/10 11:12
 */
@Slf4j
@Service
public class ElasticSearchDataSyncServiceImpl implements ElasticSearchDataSyncService {
	@Value("${spring.mq.topic.base.oms}")
	String BASE_OMS_TOPIC;
	@Resource
	ZmnMQSender zmnMQSender;

	@Override
	public void syncWorkIndex(Long orderId) {
		String key = "syncwork_" + orderId;
		if (log.isDebugEnabled()) {
			log.debug("syncwork sendmq topic:{},tag:{},key=>{},orderId=>{}", BASE_OMS_TOPIC,
					MqTagDataSyncConsts.SYNC_ORDER_WORK_TAG, key, orderId);
		}
		zmnMQSender.send(BASE_OMS_TOPIC, MqTagDataSyncConsts.SYNC_ORDER_WORK_TAG, key, JSON.toJSONString(Lists.newArrayList(orderId)));
	}

	@Override
	public void syncWorkIndex(List<Long> orderIdList) {
		String key = "syncwork_batch_" + StringUtil.getRandomStringAndNumber2(15);
		if (log.isDebugEnabled()) {
			log.debug("syncwork sendmq topic:{},tag:{},key=>{},orderIdList=>{}", BASE_OMS_TOPIC,
					MqTagDataSyncConsts.SYNC_ORDER_WORK_TAG, key, orderIdList);
		}
		zmnMQSender.send(BASE_OMS_TOPIC, MqTagDataSyncConsts.SYNC_ORDER_WORK_TAG, key, JSON.toJSONString(orderIdList));
	}

	@Override
	public void syncPartIndex(Long orderId) {
		String key = "syncpart_" + orderId;
		if (log.isDebugEnabled()) {
			log.debug("syncpart sendmq topic:{},tag:{},key=>{},orderId=>{}", BASE_OMS_TOPIC,
					MqTagDataSyncConsts.SYNC_ORDER_PART_TAG, key, orderId);
		}
		zmnMQSender.send(BASE_OMS_TOPIC, MqTagDataSyncConsts.SYNC_ORDER_PART_TAG, key, JSON.toJSONString(Lists.newArrayList(orderId)));
	}

	@Override
	public void syncPayIndex(Long orderId) {
		String key = "syncpay_" + orderId;
		if (log.isDebugEnabled()) {
			log.debug("syncpay sendmq topic:{},tag:{},key=>{},orderId=>{}", BASE_OMS_TOPIC,
					MqTagDataSyncConsts.SYNC_ORDER_PAY_TAG, key, orderId);
		}
		zmnMQSender.send(BASE_OMS_TOPIC, MqTagDataSyncConsts.SYNC_ORDER_PAY_TAG, key, JSON.toJSONString(Lists.newArrayList(orderId)));
	}


	@Override
	public void syncPerfOrderWorkIndex(Long orderId) {
		String key = "syncperfwork_" + orderId;
		if (log.isDebugEnabled()) {
			log.debug("syncperfwork sendmq topic:{},tag:{},key=>{},orderId=>{}", BASE_OMS_TOPIC,
					MqTagDataSyncConsts.SYNC_PERF_ORDER_WORK_TAG, key, orderId);
		}
		zmnMQSender.send(BASE_OMS_TOPIC, MqTagDataSyncConsts.SYNC_PERF_ORDER_WORK_TAG, key, JSON.toJSONString(Lists.newArrayList(orderId)));
	}
}
